<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ExpertController extends Controller
{
    public function index()
    {
        $ovideo=Video::where('leixing','0')->orderby('updated_at','desc')->take(3)->get();
        $latestVideo = Video::orderBy('updated_at', 'desc')->first();
        return view("front.index")->with('ovideo',$ovideo)->with('latestVideo',$latestVideo);
        //先获取直播最近的直播
//        $count=Video::where('leixing','1')->count();
//        if ($count>0){
//            $Video=Video::where('leixing',1)->orderBy('updated_at','asc')->first();//获取离当前时间最近的直播
//            $oExpert=$Video->expert()->take(2)->get();
//            return view('front.index')
//                ->with('count',$count)
//                ->with('Video',$Video)
//                ->with('oExpert',$oExpert)
//                ->with('ovideo',$ovideo);
//        }else {
//            $oExpert = Video::where('leixing', '0')->orderBy('updated_at', 'desc')->first()->expert()->take(2)->get();
//            return view('front.index')
//                ->with('count', $count)
//                ->with('oExpert', $oExpert)
//                ->with('ovideo', $ovideo);
        }
//
//
////        $expert=expert::orderby('updated_at','desc')->take(2)->get();
////        $cnt=Video::where('leixing','1')->count();
////        if ($cnt>0){
////            $Video=Video::where('leixing',1)->orderBy('updated_at','asc')->first();//获取离当前时间最近的直播
////            $expert=$ovideo->expert()->take(2)->get();
////            return view('front.index')
////                ->with('cnt',$cnt)
////                ->with('Video',$Video)
////                ->with('expert',$expert)
////                ->with('ovideo',$ovideo);
////        }
//
//
//
//
//    }
    public function create()
    {
        return view('admin.expert-create');

    }
    public function store(Request $request)
    {
        $datas = $request->all();

        $rules = [
            'name' => 'required',
            'keshi' => 'required',
            'zhicheng' => 'required',
            'zhiwu' => 'required',
            'yiyuan' => 'required',
            'introduce' => 'required',
            'education' => 'required'
        ];
        $message = [
            'name.required' => '姓名不能为空',
            'keshi.required' => '科室名不能为空',
            'zhicheng.required' => '职称不能为空',
            'zhiwu.required' => '职务不能为空',
            'yiyuan.required' => '医院不能为空',
            'introduce.required' => '简介不能为空',
            'education.required' => '接受教育不能为空',

        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{
            if($request->hasFile('thumb')){
                $thumb=$request->thumb;
                $filename = uniqid().'.'.$thumb->extension();
                $thumb->move(public_path().'/upload',$filename);
                $datas["thumb"]= '/upload/'.$filename;
            }


            Expert::create($datas);
            return json_encode(['success' => true, 'msg' => 'success']);}





    }
    public function edit($id)
    {
        $oExperts=Expert::find($id);
        return view('admin.expert-edit') ->with('oExperts',$oExperts);
    }
    public  function update($id,Request $request)
    {
        $oExperts=Expert::find($id);


        $datas = $request->all();

        $rules = [
            'name' => 'required',
            'keshi' => 'required',
            'zhicheng' => 'required',
            'zhiwu' => 'required',
            'yiyuan' => 'required',
            'introduce' => 'required',
            'education' => 'required'
        ];
        $message = [
            'name.required' => '姓名不能为空',
            'keshi.required' => '科室名不能为空',
            'zhicheng.required' => '职称不能为空',
            'zhiwu.required' => '职务不能为空',
            'yiyuan.required' => '医院不能为空',
            'introduce.required' => '简介不能为空',
            'education.required' => '接受教育不能为空',

        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails())
        {
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }else{

            if($request->hasFile('thumb')){
                $thumb=$request->thumb;
                $filename = uniqid().'.'.$thumb->extension();
                $thumb->move(public_path().'/upload',$filename);
                $oExperts["thumb"]= '/upload/'.$filename;
            }



            $oExperts->name=$request['name'];
        $oExperts->keshi=$request['keshi'];
        $oExperts->zhicheng=$request['zhicheng'];
        $oExperts->zhiwu=$request['zhiwu'];
        $oExperts->yiyuan=$request['yiyuan'];
        $oExperts->introduce=$request['introduce'];
        $oExperts->education=$request['education'];
        $oExperts->save();
        return json_encode(['success'=>true,'msg'=>"success"]);}





    }
    public function destroy($id)
    {
        $oExperts=Expert::find($id);
        $oExperts->delete();
        return json_encode(['msg'=>'删除成功']);
    }
//    public function destroy($id)
//    {
//        $oExperts=Expert::find($id);
//
//        $oExperts->delete();
//        return redirect('/adm/expertlist');
//    }
    public function ExpertList(){
        $oExperts=Expert::orderby('created_at','desc')->paginate(5);
        return view('admin.expert')->with("oExperts",$oExperts);
    }

    public function expert(){
        $expert=expert::orderby('created_at','desc')->paginate(5);
        return view('front.expert')->with('expert',$expert);
    }
    public function expertinfo($id){
        $expert=expert::find($id);
        $Oexpert=expert::where('id','!=',$id)->take(3)->get();
        return view('front.expert-info')->with('expert',$expert)->with('Oexpert',$Oexpert);
    }
}
